package org.peterbjornx.model;

import org.peterbjornx.io.RSInputStream;
import org.peterbjornx.util.TextUtils;
import org.peterbjornx.Client;

import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: PeterbjornX
 * Date: Apr 10, 2009
 * Time: 11:07:39 AM
 * >
 */
public class Player extends Entity{


    public int rights;
    public int chattype;
    public String name;
    private int gender;
    private int headIcon;
    private int team;
    private int[] equipment = new int[12];
    private int[] colors = new int[5];
    private int combatLevel;
    private short skill;
    private boolean visible;

    public void appeareanceupdate(RSInputStream stream) throws IOException {
        gender = stream.readUByte();
        headIcon = stream.readUByte();
        //desc = null;
        team = 0;
        for(int j = 0; j < 12; j++)
        {
            int k = stream.readUByte();
            if(k == 0)
            {
                equipment[j] = 0;
                continue;
            }
            int i1 = stream.readUByte();
            equipment[j] = (k << 8) + i1;
            if(j == 0 && equipment[0] == 65535)
            {
                //TODO: Implement pnpc
                //desc = EntityDef.forID(stream.readUShort());
                break;
            }
            //TODO:Implement items and teams
            /*if(equipment[j] >= 512 && equipment[j] - 512 < ItemDef.totalItems)
            {
                int l1 = ItemDef.forID(equipment[j] - 512).team;
                if(l1 != 0)
                    team = l1;
            } */
        }

        for(int l = 0; l < 5; l++)
        {
            int j1 = stream.readUByte();
            if(j1 < 0 || j1 >= clothColors[l].length)
                j1 = 0;
            colors[l] = j1;
        }

        super.idleAnimation = stream.readUShort();
        if(super.idleAnimation == 65535)
            super.idleAnimation = -1;
        super.idleturnanim = stream.readUShort();
        if(super.idleturnanim == 65535)
            super.idleturnanim = -1;
        super.walkanim = stream.readUShort();
        if(super.walkanim == 65535)
            super.walkanim = -1;
        super.turn180anim = stream.readUShort();
        if(super.turn180anim == 65535)
            super.turn180anim = -1;
        super.turn90cwanim = stream.readUShort();
        if(super.turn90cwanim == 65535)
            super.turn90cwanim = -1;
        super.turn90ccwanim = stream.readUShort();
        if(super.turn90ccwanim == 65535)
            super.turn90ccwanim = -1;
        super.runanim = stream.readUShort();
        if(super.runanim == 65535)
            super.runanim = -1;
        name = TextUtils.nameForLong(stream.readLong());
        Client.instance.maingamestate.addMessage("Your name is now "+name);
        combatLevel = stream.readUByte();
        skill = stream.readUShort();
        visible = true;
        /*aLong1718 = 0L;
        for(int k1 = 0; k1 < 12; k1++)
        {
            aLong1718 <<= 4;
            if(equipment[k1] >= 256)
                aLong1718 += equipment[k1] - 256;
        }

        if(equipment[0] >= 256)
            aLong1718 += equipment[0] - 256 >> 4;
        if(equipment[1] >= 256)
            aLong1718 += equipment[1] - 256 >> 8;
        for(int i2 = 0; i2 < 5; i2++)
        {
            aLong1718 <<= 3;
            aLong1718 += colors[i2];
        }

        aLong1718 <<= 1;
        aLong1718 += anInt1702;*///TODO:Find out what this is, some hash maybe
    }
    public static final int[][] clothColors = {
        {
            6798, 107, 10283, 16, 4797, 7744, 5799, 4634, 33697, 22433,
            2983, 54193
        }, {
            8741, 12, 64030, 43162, 7735, 8404, 1701, 38430, 24094, 10153,
            56621, 4783, 1341, 16578, 35003, 25239
        }, {
            25238, 8742, 12, 64030, 43162, 7735, 8404, 1701, 38430, 24094,
            10153, 56621, 4783, 1341, 16578, 35003
        }, {
            4626, 11146, 6439, 12, 4758, 10270
        }, {
            4550, 4537, 5681, 5673, 5790, 6806, 8076, 4574
        }
    };
}
